package com.pearadmin.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pearadmin.common.nucleus.base.BaseController;
import com.pearadmin.common.nucleus.domain.request.PageParam;
import com.pearadmin.common.nucleus.domain.response.Result;
import com.pearadmin.common.nucleus.domain.response.TableVO;
import com.pearadmin.system.domain.SysDictData;
import com.pearadmin.system.service.ISysDictDataService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.management.Query;

@RestController
@RequestMapping("system/dictData")
public class SysDictDataController  extends BaseController {

    private String MODULE_PATH = "system/dictData/";

    @Resource
    private ISysDictDataService sysDictDataService;

    @GetMapping("main")
    public ModelAndView main(ModelAndView modelAndView,String type){
        System.out.println("数据类型:"+type);
        modelAndView.addObject("type",type);
        modelAndView.setViewName(MODULE_PATH+"main");
        return modelAndView;
    }

    @GetMapping("data")
    public TableVO  data(PageParam pageParam,String type){
        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",type);
        IPage<SysDictData> pageInfo = sysDictDataService.page(new Page<>(pageParam.getPage(),pageParam.getLimit()),queryWrapper);
        return PageTable(pageInfo.getRecords(),pageInfo.getTotal());
    }

    @GetMapping("add")
    public ModelAndView add(ModelAndView modelAndView){
        modelAndView.setViewName(MODULE_PATH+"add");
        return modelAndView;
    }

    @PostMapping("save")
    public Result save(@RequestBody SysDictData sysDictData){
        boolean result = sysDictDataService.save(sysDictData);
        return choice(result);
    }

    @GetMapping("edit")
    public ModelAndView edit(ModelAndView modelAndView){
        modelAndView.setViewName(MODULE_PATH+"edit");
        return modelAndView;
    }

    @PutMapping("update")
    public Result update(@RequestBody SysDictData sysDictData){
        boolean result = sysDictDataService.updateById(sysDictData);
        return choice(result);
    }

    @DeleteMapping("deleteById/{id}")
    public Result deleteById(@PathVariable String id){
        boolean result = sysDictDataService.removeById(id);
        return choice(result);
    }
}
